import type { AppRouteRecordRaw, AppRouteModule } from '/@/router/types';
import { PageEnum } from '/@/enums/pageEnum';
import { PAGE_NOT_FOUND_ROUTE, REDIRECT_ROUTE } from '/@/router/routes/basic';

const routeModuleList: AppRouteModule[] = [];

// 内置的路由注册
const modules = import.meta.globEager('./modules/**/*.ts');
Object.keys(modules).forEach((key) => {
  const mod = modules[key].default || {};
  const modList = Array.isArray(mod) ? [...mod] : [mod];
  routeModuleList.push(...modList);
});

// 模块的路由注册
const moduleRouting = import.meta.globEager('../../**/*-routing.ts');
Object.keys(moduleRouting).forEach((key) => {
  const mod = moduleRouting[key].default || {};
  const modList = Array.isArray(mod) ? [...mod] : [mod];
  routeModuleList.push(...modList);
});

/**
 * 异步加载的路由
 */
export const asyncRoutes = [
  PAGE_NOT_FOUND_ROUTE, // 找不到页面的路由
  ...routeModuleList, // 业务模块的路由
];

/** 根路由 */
export const RootRoute: AppRouteRecordRaw = {
  path: '/',
  name: 'Root',
  redirect: PageEnum.BASE_HOME,
  meta: {
    title: 'Root',
  },
};

/** 登录路由 */
export const LoginRoute: AppRouteRecordRaw = {
  path: '/account',
  name: 'account',
  component: () => import('/@/account/account-layout.vue'),
  redirect: '/account/login',
  meta: {
    title: '登录',
  },
  children: [
    {
      path: 'login',
      // tslint:disable-next-line:object-literal-sort-keys
      name: 'login',
      component: () => import('/@/account/login/login.vue'),
      meta: { title: '登录' },
    },
    {
      path: 'tenant-register',
      name: 'tenant-register',
      component: () => import('/@/account/tenant-register/tenant-register.vue'),
      meta: { title: '租户注册' },
    },
    {
      path: 'register',
      name: 'register',
      component: () => import('/@/account/register/register.vue'),
      meta: { title: '注册' },
    },
    {
      path: 'forgot-password',
      name: 'forgot-password',
      component: () => import('/@/account/forgot-password/forgot-password.vue'),
      meta: { title: '忘记密码' },
    },
    {
      path: 'reset-password',
      name: 'reset-password',
      component: () => import('/@/account/reset-password/reset-password.vue'),
      meta: { title: '重置密码' },
    },
    {
      path: 'set-license',
      name: 'set-license',
      component: () => import('/@/project-license/set-license/set-license.vue'),
      meta: {
        title: 'LicenseSetting',
      },
    },
  ],
};

// Basic routing without permission
export const basicRoutes = [LoginRoute, RootRoute, REDIRECT_ROUTE, PAGE_NOT_FOUND_ROUTE];
